package com.xuecheng.ucenter.service.impl;

import com.xuecheng.framework.domain.ucenter.XcCompanyUser;
import com.xuecheng.framework.domain.ucenter.XcMenu;
import com.xuecheng.framework.domain.ucenter.XcUser;
import com.xuecheng.framework.domain.ucenter.ext.XcUserExt;
import com.xuecheng.ucenter.dao.XcCompanyUserRepository;
import com.xuecheng.ucenter.dao.XcMenuMapper;
import com.xuecheng.ucenter.dao.XcUserRepository;
import com.xuecheng.ucenter.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Kidy
 * @date 2020/7/15 17:49
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private XcUserRepository xcUserRepository;

    @Autowired
    private XcCompanyUserRepository xcCompanyUserRepository;

    @Autowired
    private XcMenuMapper xcMenuMapper;

    @Override
    public XcUserExt getUserExt(String username) {
        //根据账号查询xcUser
        XcUser xcUser = this.findXcUserByUsername(username);
        if (null == xcUser) {
            return null;
        }
        //用户id
        String id = xcUser.getId();
        XcCompanyUser xcCompanyUser = xcCompanyUserRepository.findByUserId(id);
        String companyId = null;
        if (null != xcCompanyUser) {
            companyId = xcCompanyUser.getCompanyId();
        }
        List<XcMenu> xcMenus = xcMenuMapper.selectPermissionByUserId(id);

        XcUserExt xcUserExt = new XcUserExt();
        BeanUtils.copyProperties(xcUser, xcUserExt);
        xcUserExt.setCompanyId(companyId);
        xcUserExt.setPermissions(xcMenus);
        return xcUserExt;
    }

    @Override
    public XcUser findXcUserByUsername(String username) {
        return xcUserRepository.findByUsername(username);
    }
}

